package com.onlinepay.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author ZouLiang
 * @Date2021/5/27 15:02
 * @Version V1.0
 * @Description 银行信息数据实体，用于保存银行通道的各种数据
 */
@Data
public class BankInfo implements Serializable {
    private int bankId;                 //银行ID
    private int currency;              //通道值
    private String currencyName;       //通道名称
    private String bankName;            //银行名称
    private String merchantID;         //我们在银行的商户号
    private String baseCode;            //币种代码
    private String sendUrl;            //发送执行路径
    private String receiveUrl;         //接收执行路径
    private String otherInfo;           //其他银行信息
    private int status; //状态(0：禁用 1：启用)
    private int product;//是否上传商品 1.上传 .0 不上传

    //上送渠道名称
    private String channelName;

    private String autofillSwitch; //自动补充

    private String companyBody; //主体

    /**
     * 根据xml的key标识拿取值，如xml为：<Access_Code>32424RMB</Access_Code>，根据传入的key值为  Access_Code ，得到值   32424RMB
      * @param key
     * @return
     */
    public String getValueFromOtherInfoByKey(String key) {
        if(key==null || key.trim().equals("") || otherInfo==null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<");
        sb.append(key.trim());
        sb.append(">");
        sb.append("(.*)");
        sb.append("</");
        sb.append(key.trim());
        sb.append(">");

        Pattern p = Pattern.compile(sb.toString());
		Matcher m = p.matcher(this.otherInfo);
		if(m.find()) {
			return m.group(1).trim();
		}
        return "";
    }

    public static void main(String[] args) {
        BankInfo info = new BankInfo();
        String otherInfo = "<SECURE_SECRET>844CBC81C56BB9B8F480CDC6F28783D7</SECURE_SECRET><ACCESS_CODE>B1213A32</ACCESS_CODE>";
        info.setOtherInfo(otherInfo);

        System.out.println(info.getValueFromOtherInfoByKey("SECURE_SECRET"));
    }

}
